package com.chinasofti.admin.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.chinasofti.manager.service.AdminEmpService;
import com.chinasofti.model.Dept;
import com.chinasofti.model.Emp;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.LinkedList;
import java.util.List;

/**
 * Creator: <br>
 * Date: <br>
 * Description: <br>
 */
@Controller
@RequestMapping("/admin/emp")
public class AdminEmpController {
    @Reference
    private AdminEmpService adminEmpService;

    @GetMapping("/add")
    public ModelAndView add(){
        ModelAndView mv = new ModelAndView("emp/add");
        List<Dept> depts = adminEmpService.queryDeptList();
        List<String> sexs = new LinkedList<>();
        sexs.add("男");
        sexs.add("女");
        mv.addObject("depts", depts);
        mv.addObject("sexs", sexs);
        return mv;
    }

    @PostMapping("/add")
    public ModelAndView add(String name, String password,
                            String gender, Integer age,
                            String phone, String deptName,
                            Double salary, String address){
        adminEmpService.addEmp(name, password, gender, age, phone, deptName, salary, address);
        return new ModelAndView("redirect:/admin/emp/list");
    }

    @GetMapping("/list")
    public ModelAndView list(){
        ModelAndView mv = new ModelAndView("emp/list");
        List<Emp> emps = adminEmpService.queryEmpList();
        mv.addObject("emps", emps);
        return mv;
    }

    @GetMapping("/delete/{id}")
    public ModelAndView delete(@PathVariable("id") Long id){
        adminEmpService.deleteEmp(id);
        return new ModelAndView("redirect:/admin/emp/list");
    }

    @GetMapping("/update/{id}")
    public ModelAndView update(@PathVariable("id") Long id){
        ModelAndView mv = new ModelAndView("emp/update");
        mv.addObject("id", id);
        List<String> sexs = new LinkedList<>();
        sexs.add("男");
        sexs.add("女");
        mv.addObject("sexs", sexs);
        return mv;
    }

    @PostMapping("/update")
    public ModelAndView update(String name, String password,
                               String gender, Integer age,
                               String phone, Double salary,
                               String address, Long id){
        adminEmpService.updateEmp(name, password, gender, age, phone, salary, address, id);
        return new ModelAndView("redirect:/admin/emp/list");
    }

}
